#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

import abc
import time

from common.common_models import LogDetail
from tpops.gaussdb.common.const import LogLevel


class ExecBase(metaclass=abc.ABCMeta):
    """
    备份、恢复、资源接入功能实现基类
    """

    def __init__(self, pid, job_id, sub_job_id, data):
        self._pid = pid
        self._job_id = job_id
        self._sub_job_id = sub_job_id
        self._std_in = data
        self._sub_job_name = ""
        self._err_code = 0
        self._logdetail = None

    @staticmethod
    def get_repository_path(file_content, repository_type):
        repositories = file_content.get("job", {}).get("repositories", [])
        repositories_path = ""
        for repository in repositories:
            if repository['repositoryType'] == repository_type:
                repositories_path = repository["path"][0]
                break
        return repositories_path

    @staticmethod
    def get_repository_path_list(file_content, repository_type):
        repositories = file_content.get("job", {}).get("repositories", [])
        repositories_path_list = []
        for repository in repositories:
            if repository['repositoryType'] == repository_type:
                repositories_path_list.append(repository["path"][0])
        return repositories_path_list

    def set_log_detail_with_params(self, log_label, sub_job_id, err_code=None, log_detail_param=None,
                                   log_level=LogLevel.INFO.value):
        err_dict = LogDetail(logInfo=log_label,
                             logInfoParam=[sub_job_id],
                             logTimestamp=int(time.time()),
                             logDetail=err_code,
                             logDetailParam=log_detail_param,
                             logLevel=log_level)
        self._logdetail = []
        self._logdetail.append(err_dict)
        return True

